package com.gzhryc.shared_device.oem.h5.mng.controllers.cdb;

import javax.servlet.http.HttpServletRequest;

import com.gzhryc.common.NumberTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.cdb.code.CDBCostConfig;
import com.gzhryc.shared_device.cdb.code.dao.db.CDBFeeTemplate;
import com.gzhryc.shared_device.cdb.code.services.CDBFeeTemplateService;
import com.gzhryc.shared_device.oem.code.ConfigUtils;
import com.gzhryc.shared_device.oem.code.business.EBusinessType;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlaceBusinessConfig;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlacePhysicalStore;
import com.gzhryc.shared_device.oem.code.place.services.PlaceBusinessConfigService;
import com.gzhryc.shared_device.oem.code.place.services.PlacePhysicalStoreService;

public class CDBDeviceCostController {

    @Route(value = { "/cdb/device/cost.html" })
    public String cost(HttpServletRequest request) {
        CDBCostConfig config = ConfigUtils.getCDBCostConfig();
        WebUtils.setAttribute(request,config);
        return "/cdb/cost/config.html";
    }

    @Route(value = { "/cdb/device/cost/save.do" })
    public AjaxResult doCostSave(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "保存失败");
        CDBCostConfig config = WebUtils.getParameter(request, CDBCostConfig.class);
        if(config != null){
            Double depositMoneyStr = WebUtils.getDouble(request,"depositMoneyStr");
            if(depositMoneyStr == null || depositMoneyStr < 0.01 || depositMoneyStr > 99){
                result.setMsg("押金设置范围在0.01-99元");
                return result;
            }
            config.setDepositMoney(NumberTools.changeMoney(depositMoneyStr));

            Double dayMaxMoneyStr = WebUtils.getDouble(request,"dayMaxMoneyStr");
            if(dayMaxMoneyStr == null || dayMaxMoneyStr < 0 || dayMaxMoneyStr > 30){
                result.setMsg("日最大费用设置范围在0.01-30元");
                return result;
            }
            config.setDayMaxMoney(NumberTools.changeMoney(dayMaxMoneyStr));

            Double payIntervalMoneyStr = WebUtils.getDouble(request,"payIntervalMoneyStr");
            if(payIntervalMoneyStr == null || payIntervalMoneyStr < 0 || payIntervalMoneyStr > 10){
                result.setMsg("每小时支付金额设置范围在0-10元");
                return result;
            }
            config.setPayIntervalMoney(NumberTools.changeMoney(payIntervalMoneyStr));

            if(ConfigUtils.save(config)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        }
        return result;
    }

    @Route(value = { "/cdb/place/cost.html" })
    public String placeCost(HttpServletRequest request) {
        Long placeId = WebUtils.getLong(request,"placeId");
        CDBFeeTemplate feeTemplate = null;
        PlaceBusinessConfig deviceFunc = PlaceBusinessConfigService.self().getByKeys(placeId, EBusinessType.CDB.index());
        if(deviceFunc != null && deviceFunc.getFeeTemplateId() != null){
            feeTemplate = CDBFeeTemplateService.self().getJoinById(deviceFunc.getFeeTemplateId());
        }

        PlacePhysicalStore place = PlacePhysicalStoreService.self().getById(placeId);
        if(place != null && place.getUserId() != null){
            request.setAttribute("userId",place.getUserId());
        }

        if(feeTemplate != null){
            WebUtils.setAttribute(request,feeTemplate);
        }else{
            CDBCostConfig costConfig = ConfigUtils.getCDBCostConfig();
            feeTemplate = CDBFeeTemplateService.buildByDefault(costConfig);
            WebUtils.setAttribute(request,feeTemplate);
        }
        return "/cdb/cost/place/config.html";
    }

    @Route(value = { "/cdb/place/cost/change.do" })
    public AjaxResult doChange(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "保存失败");
        Long placeId = WebUtils.getLong(request,"placeId");
        Long feeTemplateId = WebUtils.getLong(request,"feeTemplateId");

        try {
            if(PlaceBusinessConfigService.self().setFeeTemplateId(placeId,EBusinessType.CDB.index(),feeTemplateId)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }
}
